﻿#region

using System.ServiceProcess;

#endregion

namespace BuildMotion.MotionTracker.ServiceHost
{
    public partial class Service : ServiceBase
    {
        private static System.ServiceModel.ServiceHost motionTrackerService = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="Service"/> class.
        /// </summary>
        public Service()
        {
            InitializeComponent();
        }

        /// <summary>
        /// When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.
        /// </summary>
        /// <param name="args">Data passed by the start command.</param>
        protected override void OnStart(string[] args)
        {
            // Make sure the services are closed before attempting to instantiate/open;
            CloseServices();

            // Instantiate and open each service;
            motionTrackerService = new System.ServiceModel.ServiceHost(typeof (MotionTrackerWCFService));
            motionTrackerService.Open();
        }

        /// <summary>
        /// Closes the services.
        /// </summary>
        private static void CloseServices()
        {
            if (motionTrackerService != null)
            {
                motionTrackerService.Close();
            }
        }

        /// <summary>
        /// When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
        /// </summary>
        protected override void OnStop()
        {
            CloseServices();
        }
    }
}